<?php
class MenuAction extends CommonAction {

    public function index()
    {
        $menu = M('Menu');
        parent::page($menu->count());
        $menuData = $menu->order('id desc')->limit($this->limit)->select();

        $food = M('Food');
        $foodData = $food->order('add_time desc')->select();

        // 获取本周的所经过的日期
        $todayWeek = date('w');
        $right = (7 - $todayWeek) * 3600 * 24;
        $left = ($todayWeek - 1) * 3600 * 24;

        $endDate = date('Y-m-d', (time() + $right));
        $startDate = date('Y-m-d', (time() - $left));

        // 判断是否已经保存本周的菜单
        $toWeekMenu = $menu->where("menu_start_date = '$startDate' and menu_end_date = '$endDate'")->find();
        $toWeekMenu['menu_content'] = unserialize($toWeekMenu['menu_content']);

        // 是增加还是修改菜单
        $operation = $toWeekMenu['id'] ? 'update' : 'add';

        $menuContent = array();
        if(isset($_POST['send']))
        {
            $temp = array('food_content_1', 'food_content_2', 'food_content_3', 'food_content_4', 'food_content_5', 'food_content_6', 'food_content_7');
            foreach($temp as $k=>$v)
            {
                $menuContent[$v] = $this->_post($v);
            }

            foreach($menuContent as $k=>&$v)
            {
                $v = array_filter($v, array($this, '_filterNone'));
            }

            $data['menu_start_date'] = $startDate;
            $data['menu_end_date'] = $endDate;
            $data['menu_content'] = serialize($menuContent);
            $data['add_user'] = session('admin');

            if($operation == 'update')
            {
                $data['id'] = $this->_post('id');
                $menu->save($data);
                redirectByModal('修改菜单成功!', self::$SUCCESS, __URL__ . '/index');
            }
            else if($operation == 'add')
            {
                if ($menu->add($data))
                {
                    redirectByModal('确定菜单成功!', self::$SUCCESS, __URL__ . '/index');
                }
            }
        }

        $this->assign(
            array(
                'menuData' => $menuData,
                'foodData' => $foodData,
                'tdList' => array(1, 2, 3, 4, 5, 6, 7),
                'startDate' => $startDate,
                'endDate' => $endDate,
                'toWeekMenu' => $toWeekMenu['menu_content'],
                'id' => $toWeekMenu['id'],
                'operation' => $operation,
            )
        );

        $this->display();
    }

    // 形成采购清单
    public function createOrder($id)
    {
        $food = M('Food');
        $menu = M('Menu');
        $stuff = M('Stuff');

        $menuData = $menu->where('id='.$id)->find();
        $menuData['menu_content'] = unserialize($menuData['menu_content']);

        $purchaseContent = array();

        // 分解菜单内容
        foreach($menuData['menu_content'] as $k=>$v)
        {
            $purchaseContent[$k] = array();
            foreach($v as $k2=>$v2) {
                $data = $food->where('id='.$v2)->find();

                $purchaseContent[$k][$data['id']]['price_count'] = $data['food_price_count'];

                $main = unserialize($data['food_main_part']);
                $sur = unserialize($data['food_sur_part']);

                $stuffData = array_merge($main, $sur);
                foreach ($stuffData as $k3=>$v3)
                {
                    $oneStuff = $stuff->field(array('stuff_name', 'stuff_price'))->where('id='.$v3['food'])->find();
                    $purchaseContent[$k][$data['id']]['stuff'][] = array($v3['food'], $oneStuff['stuff_name'], $v3['num'], $v3['unit'], $oneStuff['stuff_price'], rand(2, 300));
                }
            }
        }
        $dateArr = array(1, 2, 3, 4, 5, 6, 7);

        $startDate = strtotime($menuData['menu_start_date']);

        $createDate = array();

        $purchase = M('Purchase');
        for($i=0; $i<7; $i++)
        {
            $datas['purchase_content'] = serialize($purchaseContent['food_content_'.($i+1)]);
            $datas['purchase_date'] = date('Y-m-d', $startDate + 3600 * 24 * $i);
            $datas['supplier_id'] = 1;

            $purchase->add($datas);
        }

        $this->success('确定采购清单成功，请到库存模块查看', __URL__.'/index');
    }

    // 查看历史菜单
    public function showHistory()
    {
        $menu = M('Menu');

        // 获取本周的所经过的日期
        $todayWeek = date('w');
        $right = (7 - $todayWeek) * 3600 * 24;
        $left = ($todayWeek - 1) * 3600 * 24;

        $endDate = date('Y-m-d', (time() + $right));
        $startDate = date('Y-m-d', (time() - $left));

        // 判断是否已经保存本周的菜单
        parent::page($menu->where("menu_start_date != '$startDate' and menu_end_date != '$endDate'")->count());
        $historyMenu = $menu->where("menu_start_date != '$startDate' and menu_end_date != '$endDate'")
                            ->limit($this->limit)
                            ->order('id desc')
                            ->select();

        $food = M('Food');
        $stuff = M('Stuff');


        foreach($historyMenu as $k=>&$v)
        {
            $priceCount = 0;

            $v['menu_content'] = unserialize($v['menu_content']);

            foreach($v['menu_content'] as $k2=>&$v2)
            {
                foreach($v2 as $k3=>&$v3)
                {
                    $data = $food->field(array('food_name', 'food_main_part', 'food_sur_part'))->where('id='.$v3)->find();
                    $v3 = $data['food_name'];
                    $main = unserialize($data['food_main_part']);
                    $sur = unserialize($data['food_sur_part']);

                    $stuffData = array_merge($main, $sur);

                    foreach($stuffData as $k4=>$v4)
                    {
                        $priceCount += $v4['num'] * $v4['price'];
                    }
                }
            }
            $v['priceCount'] = $priceCount;
        }

        $this->assign('historyMenu', $historyMenu);
        $this->display();
    }

    // 过滤没有选中的菜品菜单项
    private function _filterNone($var)
    {
        return $var != -1;
    }
}